﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Dto.Customer;
using CRM.Admin.Contracts.Model.Customer;
using CRM.Admin.Services.Service.Customer;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;
using KingsSharp.Common.Filter;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Web.UI;
using KingsSharp.Core.Data.Extensions;
using CRM.Admin.Contracts.Contract.Sys;

namespace CRM.Admin.Areas.Customer.Controllers
{
    public class SteelController : Controller
    {
        /// <summary>
        /// 钢厂业务契约
        /// </summary>
        public ISteelService SteelService { get; set; }
        /// <summary>
        /// 用户业务契约
        /// </summary>
        public IUserInfoService UserInfoService { get; set; }
        /// <summary>
        /// 是否具备某个页面的权限
        /// </summary>
        /// <param name="actionName">页面名称</param>
        /// <param name="controllerName">控制器名称</param>
        /// <param name="operaName">操作名称</param>
        /// <returns></returns>
        public bool Graent(string actionName, string controllerName, string operaName)
        {
            return UserInfoService.GetUserForPageGarent(actionName, controllerName, operaName, AppVariable.UserData.UserId);
        }
        #region 视图功能
        public ActionResult SteelIndex()
        {
            ViewBag.createSteelgarent = Graent("SteelIndex", "Steel", "insert");
            ViewBag.updateSteelgarent = Graent("SteelIndex", "Steel", "update");
            ViewBag.deleteSteelgarent = Graent("SteelIndex", "Steel", "delete");
            return View();
        }

        /// <summary>
        /// 钢厂新增
        /// </summary>
        /// <returns></returns>
        public ActionResult SteelAdd()
        {
            return View();
        }

        public ActionResult SteelEdit(Guid SteelId)
        {
            var dto = SteelService.Steels.FirstOrDefault(entity => entity.Id == SteelId).MapTo<SteelDto>();
            return View(dto);
        }

        /// <summary>
        /// 钢厂选择
        /// </summary>
        /// <param name="Multi">是否允许多选，默认false</param>
        /// <returns></returns>
        public ActionResult SteelCheck(bool Multi = false)
        {
            ViewBag.Multi = Multi.ToString().ToLower();
            return View();
        }

        #endregion

        #region 数据功能

        public ActionResult SteelInsert(SteelDto steelDto)
        {
            steelDto.CheckNotNull("steelDto");

            var result = SteelService.Insert(steelDto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "钢厂 “{0}” 添加成功。".FormatWith(steelDto.Name);
            }
            return Json(new AjaxResult(msg, msgType));
        }

        public ActionResult SteelUpdate(SteelDto steelDto)
        {
            steelDto.CheckNotNull("steelDto");

            var result = SteelService.Update(steelDto);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "钢厂 “{0}” 更新成功。".FormatWith(steelDto.Name);
            }
            return Json(new AjaxResult(msg, msgType));
        }
        public ActionResult SteelDelete(Guid SteelId,string SteelName)
        {
            var result = SteelService.Delete(SteelId);
            string msg = result.Message ?? result.ResultType.ToDescription();
            AjaxResultType msgType = result.ResultType.ToAjaxResultType();
            if (msgType != AjaxResultType.Error)
            {
                msg = "钢厂“{0}”删除成功。".FormatWith(SteelName);
            }
            return Json(new AjaxResult(msg, msgType));
        }

        public ActionResult GetSteelGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<Steel, bool>> predicate = FilterHelper.GetExpression<Steel>(request.FilterGroup);
            int total;
            var data = SteelService.Steels.
                Where<Steel, Guid>(predicate, request.PageCondition, out total).
                Where(a => a.IsDeleted == false).
                Select(dto => new
                {
                    dto.Id,
                    dto.Name
                }).ToList();
            return Json(new GridData<object>(data, total), JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}